package com.sharker.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.sharker.R;
import com.sharker.application.App;
import com.sharker.models.data.SessionModel;
import com.sharker.network.SharkerParams;
import com.sharker.utils.RegexUtils;
import com.sharker.view.Toptitlebar;

import org.xutils.common.Callback;
import org.xutils.x;


public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView text0;
    private Button send;
    private Toptitlebar regist_titlebar;
    private EditText regist_phone;
    private EditText regist_num;
    private TextView regist_getnum;
    private EditText regist_password;
    private Button regist_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regist_layout);
        initView();
    }

    private void initView() {

        regist_titlebar = (Toptitlebar) findViewById(R.id.regist_titlebar);
        regist_phone = (EditText) findViewById(R.id.regist_phone);
        regist_phone.setOnClickListener(this);
        regist_num = (EditText) findViewById(R.id.regist_num);
        regist_num.setOnClickListener(this);
        regist_getnum = (TextView) findViewById(R.id.regist_getnum);
        regist_getnum.setOnClickListener(this);
        regist_password = (EditText) findViewById(R.id.regist_password);
        regist_password.setOnClickListener(this);
        regist_btn = (Button) findViewById(R.id.regist_btn);
        regist_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_btn:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String phone = regist_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone) || !RegexUtils.isMobileExact(phone)) {
            Toast.makeText(this, "+86", Toast.LENGTH_SHORT).show();
            return;
        }

        String num = regist_num.getText().toString().trim();
        if (TextUtils.isEmpty(num)) {
            Toast.makeText(this, "输入你收到的验证码", Toast.LENGTH_SHORT).show();
            return;
        }

        String password = regist_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码不少于6个字", Toast.LENGTH_SHORT).show();
            return;
        }
        SharkerParams sharkerParams = new SharkerParams("user_reg");
        sharkerParams.addBodyParameter("mobile", phone);
        x.http().post(sharkerParams, new Callback.CommonCallback<SessionModel>() {
            @Override
            public void onSuccess(SessionModel result) {
                Log.i("TAG", "onSuccess: ++++" + result.toString());
                ToastUtils.showShortToast(result.data.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        // TODO validate success, do something
       

    }
}
